const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    icon: cc.Node = null;
    mid: cc.Node = null;
    up: cc.Node = null;
    down: cc.Node = null;
    left: cc.Node = null;
    right: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}


    start() {
        this.icon = this.node.getChildByName("icon");

        this.mid = this.node.getChildByName("mid");
        this.up = this.node.getChildByName("up");
        this.down = this.node.getChildByName("down");

        this.left = this.node.getChildByName("left");
        this.right = this.node.getChildByName("right");
    }

    // update (dt) {}
    // dir = 1, 右边跳跃, -1 表示左边跳跃
    //wdst 要跳跃的位置
    is_jump_on_block(w_dst_pos: cc.Vec3, direction: number) {

        let mid_pos = this.mid.convertToWorldSpaceAR(cc.v3(0, 0));
        // 中心点距离方向
        let dir = w_dst_pos.sub(mid_pos);
        // 跳跃距离
        let min_len = dir.mag();

        let min_pos = mid_pos;
        // 跳到中间得分多
        let jumpScore = 2;

        let len = 0;
        if (direction === 1) {
            cc.log('向上跳跃')
            let up_pos = this.up.convertToWorldSpaceAR(cc.v3(0, 0));
            dir = w_dst_pos.sub(up_pos);
            len = dir.mag();
            if (min_len > len) {
                min_len = len;
                min_pos = up_pos;
                jumpScore = 1;
            }
            //向下跳跃
            cc.log('向下跳跃')
            let down_pos = this.down.convertToWorldSpaceAR(cc.v3(0, 0));
            dir = w_dst_pos.sub(down_pos);
            len = dir.mag();
            if (min_len > len) {
                min_len = len;
                min_pos = down_pos;
                jumpScore = 1;
            }
        }
        else {
            //向左跳跃
            cc.log('向左跳跃')
            let left_pos = this.left.convertToWorldSpaceAR(cc.v3(0, 0));
            dir = w_dst_pos.sub(left_pos);
            len = dir.mag();
            if (min_len > len) {
                min_len = len;
                min_pos = left_pos;
                jumpScore = 1;
            }
            //向右跳跃
            cc.log('向右跳跃')
            let right_pos = this.right.convertToWorldSpaceAR(cc.v3(0, 0));
            dir = w_dst_pos.sub(right_pos);
            len = dir.mag();
            if (min_len > len) {
                min_len = len;
                min_pos = right_pos;
                jumpScore = 1;
            }
        }

        // 找到了跳跃的位置距离参考点最近的那个参考点以及位置;
        dir = w_dst_pos.sub(min_pos);
        if (dir.mag() < 100) {
            w_dst_pos.x = min_pos.x;
            w_dst_pos.y = min_pos.y;
            return jumpScore;
        }
        // end 
        return 0;
    }

    //分数计算
    isJumpOnBlock(w_dst_pos: cc.Vec3, direction: number) {
        if (direction === 1) {
            //向上跳跃
            return this.jumpDir(this.up, w_dst_pos);
        }
        else {
            return this.jumpDir(this.left, w_dst_pos);
        }
    }

    jumpDir(jumpNode: cc.Node, w_dst_pos: cc.Vec3) {
        let mid_pos = this.mid.convertToWorldSpaceAR(cc.v3(0, 0))
        // 中心点距离方向
        let dir = w_dst_pos.sub(mid_pos);
        // 跳跃距离
        let min_len = dir.mag();
        // 最小距离
        let min_pos = mid_pos;
        // 跳到中间得分多
        let jumpScore = 2;

        //跳跃计算
        let jump_pos = jumpNode.convertToWorldSpaceAR(cc.v3());
        dir = w_dst_pos.sub(jump_pos);
        //跳跃的距离
        let len = dir.mag();
        if (min_len > len) {
            min_len = len;
            min_pos = jump_pos;
            jumpScore = 1;
        }
        dir = w_dst_pos.sub(min_pos);
        if (dir.mag() < 100) {
            w_dst_pos.x = min_pos.x;
            w_dst_pos.y = min_pos.y;
            return jumpScore;
        }
        return 0;
    }
}
